<script setup>
//定义的各种数据
import {reactive} from "vue";
import xx from "@/axios/xx";
import {ElMessage} from "element-plus";
import {Plus} from "@element-plus/icons-vue";

const info = reactive({
  list:[],//列表数据
  form:{
    userType: ''
  },//表单数据
  dialogVisible:false, //对话框数据
  pageNum: 1, //当前页
  pageSize: 5, //每页大小
  total: 0, //总条数
  isAdd: true, //增加和修改标识
  uploadUrl:'',  //上传的地址
  dialogVisibleImages:false,  //大图展示
  dialogImageUrl:''  //大图地址
})

info.uploadUrl=xx.defaults.baseURL+'upload/singleImage'

//图片上传之前
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('只能上传图片类型!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片大小不超过2MB!')
    return false
  }
  return true
}

//图片上传成功之后
const handleAvatarSuccess = (response,uploadFile) => {
  info.form.avatar = response
}

const viewBigAvatar = (avatar) =>{
  info.dialogVisibleImages=true
  info.dialogImageUrl=avatar
}

//查询列表信息
const load = () => {
  xx.get('/userInfo',{
    params:{
      pageNum:info.pageNum,
      pageSize:info.pageSize,
      fullName:info.form.fullName,
      username:info.form.username,
      userType:info.form.userType,
      start:info.form.start,
      end:info.form.end,

    }
  }).then(res =>{
    console.log(res)
    info.list = res.data.list
    //当前页
    info.pageNum = res.data.pageNum
    //每页大小
    info.pageSize = res.data.pageSize
    //总条数
    info.total = res.data.total
  })
}
load()

//分页当前页改变
const handleCurrentChange = (pageNum) => {
  info.pageNum = pageNum
  load()
}
//每页大小
const handleSizeChange = (pageSize) => {
  info.pageSize = pageSize
  load()
}



//增加按钮点击方法
const add = () => {
  info.dialogVisible=true
  info.isAdd=true
}
//关闭对话框，清除数据
const handleClose = () => {
  info.form={
    userType:''
  }
  info.dialogVisible=false
}

//持久化数据
const persistence = () => {
  xx({
    url:'/userInfo',
    method:info.isAdd?'post':'put',
    data:info.form
  }).then(res=>{
    if(res.code === 200){
      ElMessage({
        message: res.message,
        type: 'success',
      })
    }else{
      ElMessage.error(res.message)
    }
    //关闭对话框
    info.dialogVisible=false
    info.form={}  //清空数据
    load()
  })
}


//点击修改按钮-根据id查询对应数据
const handleEdit = (userId) => {
  info.isAdd = false
  xx.get('userInfo/' + userId).then(res => {
    info.form = res.data
    info.dialogVisible = true
  })
}

//删除
const handleDelete = (userId) => {
  xx.delete('/userInfo/'+userId).then(res=>{
    console.log(res)
    if(res.code === 200){
      ElMessage({
        message: res.message,
        type: 'success',
      })
    }else{
      ElMessage.error(res.message)
    }
    load()
  })
}

</script>

<template>
  <!--增加按钮-->
  <el-button type="success" @click="add">增加用户</el-button>
  <br><br>
  <!--快捷查询-->
  <el-form :inline="true" :model="info.form" class="demo-form-inline">
    <el-form-item label="用户名称">
      <el-input v-model="info.form.fullName" placeholder="模糊查询" clearable />
    </el-form-item>
    <el-form-item label="登录用户">
      <el-input v-model="info.form.username" placeholder="精准查询" clearable />
    </el-form-item>
    <el-form-item label="用户类型">
      <el-radio-group v-model="info.form.userType">
        <el-radio value="">全部</el-radio>
        <el-radio value="普通会员">普通会员</el-radio>
        <el-radio value="内部人员">内部人员</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="创建时间">
      <el-date-picker
          v-model="info.form.start"
          type="datetime"
          placeholder="开始时间"
          format="YYYY-MM-DD hh:mm:ss"
          value-format="YYYY-MM-DD hh:mm:ss"
      />
      &nbsp;-&nbsp;
      <el-date-picker
          v-model="info.form.end"
          type="datetime"
          placeholder="结束时间"
          format="YYYY-MM-DD hh:mm:ss"
          value-format="YYYY-MM-DD hh:mm:ss"
      />
    </el-form-item>


    <el-form-item>
      <el-button type="primary" @click="load">快捷查询</el-button>
    </el-form-item>
  </el-form>
  <!--表格信息-->
  <el-table :data="info.list" stripe border  style="width: 100%">
    <el-table-column fixed type="index" align="center" label="序号" width="80" />
    <el-table-column fixed prop="fullName" align="center" label="用户全名" width="180" />
    <el-table-column prop="email" align="center" label="用户邮箱" width="180" />
    <el-table-column prop="phoneNumber" align="center" label="用户电话号码" width="180" />
    <el-table-column prop="address" align="center" label="用户地址" width="180" />
    <el-table-column prop="gender" align="center" label="用户性别" width="180" />
    <el-table-column prop="avatar" align="center" label="用户头像" width="180" >
      <template #default="scope">
        <el-image style="height: 50px;cursor: pointer" @click="viewBigAvatar(scope.row.avatar)" :src="scope.row.avatar">

        </el-image>

      </template>
    </el-table-column>
    <el-table-column prop="companyId" align="center" label="所属公司ID" width="180" />
    <el-table-column prop="createdAt" align="center" label="记录创建时间" width="180" />
    <el-table-column prop="username" align="center" label="用户账号" width="180" />
    <el-table-column prop="password" align="center" label="用户密码" width="180" />
    <el-table-column prop="userType" align="center" label="用户类型" width="180" />
    <el-table-column fixed="right" align="center" label="操作" width="200">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row.userId)">修改</el-button>
        <el-popconfirm @confirm="handleDelete(scope.row.userId)" title="你确定要删除么?">
          <template #reference>
            <el-button size="small" type="danger">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!--分页-->
  <br>
  <el-pagination
      v-model:current-page="info.pageNum"
      v-model:page-size="info.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :small="false"
      :disabled="false"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="info.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />
  <!--窗口-->
  <el-dialog
      v-model="info.dialogVisible"
      :title="info.isAdd ? '增加用户':'修改用户'"
      width="65%"
      :before-close="handleClose"
  >

    <el-form :model="info.form"  label-width="auto" style="max-width: 90%;margin-left: 20px">
      <el-row>
        <el-form-item label="用户名称">
          <el-input v-model="info.form.fullName" />
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="info.form.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="info.form.password" type="password"/>
        </el-form-item>
      </el-row>
      <el-form-item label="头像">

        <el-upload
            class="avatar-uploader"
            :action=info.uploadUrl
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
        >
          <img v-if="info.form.avatar" :src="info.form.avatar" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-row>
        <el-form-item label="邮箱">
          <el-input v-model="info.form.email" />
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="info.form.phoneNumber" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="info.form.gender">
            <el-radio value="男">男</el-radio>
            <el-radio value="女">女</el-radio>
            <el-radio value="其他">其他</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row >
        <el-form-item label="所属单位">
          <el-input v-model="info.form.companyId" />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
              v-model="info.form.createdAt"
              type="datetime"
              placeholder="选择时间"
              format="YYYY-MM-DD hh:mm:ss"
              value-format="YYYY-MM-DD hh:mm:ss"
          />
        </el-form-item>
        <el-form-item label="用户类型">
          <el-radio-group v-model="info.form.userType">
            <el-radio value="普通会员">普通会员</el-radio>
            <el-radio value="内部人员">内部人员</el-radio>
            <el-radio value="其他">其他</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-form-item label="地址">
        <el-input v-model="info.form.address" />
      </el-form-item>

    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="persistence">
          保存信息
        </el-button>
      </div>
    </template>
  </el-dialog>

  <!--大图展示-->
  <el-dialog v-model="info.dialogVisibleImages">
    <img w-full :src="info.dialogImageUrl" style="width: 100%;" alt="Preview Image"/>
  </el-dialog>


</template>


<style scoped>
.el-from-item{
  margin-left: 20px;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>